#include <iostream>
#include <time.h>
#include "BlockQueue.hpp"
#include "Thread.hpp"

void Consumer(void* args)
{
    BlockQueue<Task>* bg = (BlockQueue<Task>*)args;
    while(true)
    {
        Task t;
        bg->Pop(&t);
        t();
    }
}

void Product(void* args)
{
    BlockQueue<Task>* bg = (BlockQueue<Task>*)args;
    while(true)
    {
        int x = rand() % 10;
        usleep(rand() % 123);
        int y = rand() % 10;
        usleep(rand() % 123);
        char op = str[rand() % 2];

        Task t(x, y, op);
        bg->Push(t);
    }
}

int main()
{
    srand((unsigned)time(nullptr) ^ getpid() ^ pthread_self());
    BlockQueue<Task> bg;
    Thread<void*> t1("Consumer", Consumer, &bg);
    Thread<void*> t2("Product", Product, &bg);

    //启动线程
    t1.Start();
    t2.Start();

    //回收线程资源
    t1.Join();
    t2.Join();
    return 0;
}